package com.eightbears.beboy.adapter;

import android.text.TextUtils;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.chad.library.adapter.base.BaseMultiItemQuickAdapter;
import com.chad.library.adapter.base.BaseViewHolder;
import com.eightbears.beboy.R;
import com.eightbears.beboy.base.util.CommonUtils;
import com.eightbears.beboy.base.util.GetCategory;
import com.eightbears.beboy.entity.PayOrderEntity;
import com.makeramen.roundedimageview.RoundedImageView;

import static com.eightbears.beboy.base.util.CommonAPI.STATUS_ORDER_APPLY_COMMENT;
import static com.eightbears.beboy.base.util.CommonAPI.STATUS_ORDER_APPLY_REFOUND;
import static com.eightbears.beboy.base.util.CommonAPI.STATUS_ORDER_REFOUND_SUCCESS;
import static com.eightbears.beboy.base.util.CommonAPI.STATUS_ORDER_USE;
import static com.eightbears.beboy.base.util.CommonAPI.STATUS_ORDER_WAIT_PAY;
import static com.eightbears.beboy.base.util.CommonAPI.STATUS_UNUSE;
import static com.eightbears.beboy.entity.PayOrderEntity.PayOrder.ITEM_COMMODITY;
import static com.eightbears.beboy.entity.PayOrderEntity.PayOrder.ITEM_POST;
import static com.eightbears.beboy.entity.PayOrderEntity.PayOrder.ITEM_SET_MEAL;

public class PayOrderAdapter extends BaseMultiItemQuickAdapter<PayOrderEntity.PayOrder, BaseViewHolder> {
    private String btnText;
    private String btnTag;
    private ImageView statusImg;
    private TextView eventBtn;


    public PayOrderAdapter() {
        super(null);
        addItemType(ITEM_POST, R.layout.item_order_post);
        addItemType(ITEM_COMMODITY, R.layout.item_order_commodity);
        addItemType(ITEM_SET_MEAL, R.layout.item_order);
    }


    @Override
    protected void convert(BaseViewHolder helper, PayOrderEntity.PayOrder item) {
        switch (helper.getItemViewType()) {
            case ITEM_POST:
                helper.setText(R.id.tv_nickname, item.getObject().getUser().getNickname())
                        .setText(R.id.tv_category, GetCategory.getCategoryNameById(item.getObject().getCategory()))
                        .setText(R.id.tv_content, item.getObject().getPost())
                        .setText(R.id.tv_price, String.format(mContext.getString(R.string.text_price), item.getObject().getPrice()))
                        .addOnClickListener(R.id.tv_read).addOnClickListener(R.id.rl_item);

                if (null != item.getObject().getImages() && item.getObject().getImages().size() > 0) {
                    CommonUtils.loadImageView(mContext, item.getObject().getImages().get(0),
                            R.mipmap.default_icon, (ImageView) helper.getView(R.id.iv_user_head));
                }

                break;
            case ITEM_COMMODITY:
                if (null != item.getObject()) {
                    helper.setText(R.id.tv_title, item.getObject().getTitle())
                            .setText(R.id.tv_price, String.format(mContext.getString(R.string.text_price), item.getObject().getMoney()));
                    if (item.getObject().getCover() != null) {
                        CommonUtils.loadImageView(mContext, item.getObject().getCover(),
                                R.mipmap.default_icon, (ImageView) helper.getView(R.id.iv_commodity));
                    }
                } else {
                    helper.setText(R.id.tv_title, "此订单异常,更多详情情联系客服");
                }
                break;
            case ITEM_SET_MEAL:
                statusImg = helper.getView(R.id.iv_shopbook_use);
                eventBtn = helper.getView(R.id.tv_shopbook_mealbug);
                replaceView();

                if (item.getObject() == null) {
                    CommonUtils.loadImageView(mContext, "", R.mipmap.icon_default_shop,
                            (RoundedImageView) helper.getView(R.id.iv_shopbook_image));
                    helper.setText(R.id.tv_shopbook_mealname, "店铺异常")
                            .setText(R.id.tv_shopbook_mealprice, "")
                            .setText(R.id.tv_shopbook_mealoldprice, "")
                            .setText(R.id.tv_shopbook_mealbug, btnText)
                            .setText(R.id.tv_date, "")
                            .setText(R.id.iv_shopbook_left, "");

                } else {
                    CommonUtils.loadImageView(mContext, item.getObject().getCover(), R.mipmap.icon_default_shop,
                            (RoundedImageView) helper.getView(R.id.iv_shopbook_image));

                    if (!(TextUtils.isEmpty(item.getObject().getStatus()))) {
                        if (item.getObject().getStatus().equals(STATUS_UNUSE)) {//未使用
                            btnText = "查看";
                            btnTag = STATUS_UNUSE;
                            showUse();
                        } else if (item.getObject().getStatus().equals(STATUS_ORDER_USE)) {//已使用
                            btnText = "查看";
                            btnTag = STATUS_ORDER_USE;
                            showUse();
                        } else if (item.getObject().getStatus().equals(STATUS_ORDER_APPLY_REFOUND)) {
                            btnText = "退款中";
                            btnTag = STATUS_ORDER_APPLY_REFOUND;
                            showRefund();
                        } else if (item.getObject().getStatus().equals(STATUS_ORDER_REFOUND_SUCCESS)) {
                            btnText = "已退款";
                            btnTag = STATUS_ORDER_REFOUND_SUCCESS;
                            showRefundSuccess();
                        } else if (item.getObject().getStatus().equals(STATUS_ORDER_APPLY_COMMENT)) {
                            btnText = "查看";
                            btnTag = STATUS_ORDER_APPLY_COMMENT;
                            showUse();
                        } else if (item.getStatus().equals(STATUS_ORDER_WAIT_PAY)) {
                            btnText = "待支付";
                            btnTag = STATUS_ORDER_WAIT_PAY;
                            showWaitPay();
                        }
                    }

                    String startDate = CommonUtils.TimeStamp2Date(item.getObject().getStart_at() + "", "yyyy-MM-dd");
                    String endDate = CommonUtils.TimeStamp2Date(item.getObject().getEnd_at() + "", "yyyy-MM-dd");
                    String dateString = String.format(mContext.getResources().getString(R.string.text_valid_date), startDate, endDate);
                    //        String Result = Double.valueOf(orderInfo.getCoupon().getDiscount_price());
                    helper.setText(R.id.tv_shopbook_mealname, item.getObject().getTitle())
                            .setText(R.id.tv_shopbook_mealprice, "￥" + item.getReceipt_amount())
                            .setText(R.id.tv_shopbook_mealoldprice, "原价:￥" + item.getObject().getPrice())
                            .setText(R.id.tv_shopbook_mealbug, btnText)
                            .setText(R.id.tv_date, dateString)
                            .setText(R.id.iv_shopbook_left, item.getObject().getShop().getTitle())
                            .setTag(R.id.tv_shopbook_mealbug, btnTag)
                            .addOnClickListener(R.id.tv_shopbook_mealbug)
                            .addOnClickListener(R.id.shop_info_meal);
                    break;

                }
        }
    }

    private void showUse() {
        eventBtn.setVisibility(View.VISIBLE);
        eventBtn.setBackgroundResource(R.drawable.background_circle_bead_blue_style);
        eventBtn.setTextColor(mContext.getResources().getColor(R.color.text_color_fff));
    }

    private void showWaitPay() {
        eventBtn.setVisibility(View.VISIBLE);
        eventBtn.setBackgroundResource(R.drawable.bg_pay_red_style);
        eventBtn.setTextColor(mContext.getResources().getColor(R.color.text_color_fff));

    }

    /**
     * 显示退款中
     */
    private void showRefund() {
        eventBtn.setBackgroundResource(R.drawable.bg_gray_style);
        eventBtn.setTextColor(mContext.getResources().getColor(R.color.text_color_fff));
        eventBtn.setVisibility(View.VISIBLE);
    }

    /**
     * 显示已退款
     */
    private void showRefundSuccess() {
        eventBtn.setBackgroundResource(R.color.text_color_fff);
        eventBtn.setTextColor(mContext.getResources().getColor(R.color.text_other_info_color));
        eventBtn.setVisibility(View.VISIBLE);
        statusImg.setVisibility(View.GONE);
    }


    private void replaceView() {
        statusImg.setVisibility(View.GONE);
        eventBtn.setVisibility(View.GONE);
    }
}